<?php
/// Copyright (c) 2004-2007, Cain Chen / Hina :: 工程幼稚園
/// All rights reserved. Licensed under the GPL.
/// See the GNU General Public License for more details. (/doc/LICENSE, /doc/COPYRIGHT)

# Locale language file support, defualt is en-us.php
if (!isset($blog['blogLanguage'])) {
	$blog['blogLanguage'] = $service['language'];
}

if(isset($__ts)) unset($__ts);
$__ts = array();
// Locale language file support.
switch($blog['blogLanguage']) {
	case "zh-TW":
	case "zh-CN":
		$languageFile = $pluginPath.'/lang/'.$blog['blogLanguage'].'.php';
	break;
	default:
		$languageFile = "";
}
if(!empty($languageFile) && file_exists($languageFile)) { include ($languageFile); }

function deleteUserSignup() {
	global $database;

	if( !Acl::check( array("group.creators", "group.administrators") ) ) {
		getRespondResultPage(0);
		return;
	}
	$loginid = urldecode($_GET['loginid']);
	DBQuery::query("DELETE FROM {$database['prefix']}UsersignupLogs WHERE loginid='{$loginid}' LIMIT 1");
	getRespondResultPage(1);
}

function createTeamUserSignup() {
	global $blogid, $database;

	if( !Acl::check( array("group.creators", "group.administrators") ) ) {
		getRespondResultPage(0);
		return;
	}
	$loginid = urldecode($_GET['loginid']);
	$row = DBQuery::queryRow("SELECT * FROM {$database['prefix']}UsersignupLogs WHERE loginid='{$loginid}' LIMIT 1");
	$loginid = $row['loginid'];
	$password = $row['password'];
	$name = $row['name'];
	$created = $row['created'];
	$host2blogid = $row['host'];
	/* MAKE sure the host max value */
	$host = intval(DBQuery::queryCell("select max(host) from {$database['prefix']}Users"))+1;

	/* Insert new user into Users */
	$lastLogin = time();
	DBQuery::query("INSERT INTO {$database['prefix']}Users (loginid,password,name,created,lastLogin,host) VALUES ('$loginid', '$password', '$name', '$created', '$lastLogin', '$host')");
	$userid = DBQuery::insertId();
	if($userid>1) {
		/* Insert new user into Teamblog */
		DBQuery::query("INSERT INTO {$database['prefix']}Teamblog (blogid,userid,acl,created,lastLogin) VALUES ('$host2blogid','$userid','1','$created','$lastLogin')");
		DBQuery::query("DELETE FROM {$database['prefix']}UsersignupLogs WHERE loginid='{$loginid}' LIMIT 1");
		getRespondResultPage(1);
	} else {
		getRespondResultPage(0);
	}
}

function createUserSignup() {
	global $blogid, $database;

	if( !Acl::check( array("group.creators") ) ) {
		getRespondResultPage(0);
		return;
	}
	$loginid = urldecode($_GET['loginid']);
	$row = DBQuery::queryRow("SELECT * FROM {$database['prefix']}UsersignupLogs WHERE loginid='{$loginid}' LIMIT 1");
	$loginid = $row['loginid'];
	$password = $row['password'];
	$name = $row['name'];
	$created = $row['created'];
	/* MAKE sure the host max value */
	$host = intval(DBQuery::queryCell("select max(host) from {$database['prefix']}Users"))+1;

	/* Insert new user into Users */
	$lastLogin = time();
	DBQuery::query("INSERT INTO {$database['prefix']}Users (loginid,password,name,created,lastLogin,host) VALUES ('$loginid', '$password', '$name', '$created', '$lastLogin', '$host')");
	$userid = DBQuery::insertId();
	if($userid>1) {
		/* Insert new user into Teamblog */
		$newBlogId = intval(DBQuery::queryCell("SELECT MAX(blogid) FROM {$database['prefix']}Teamblog"))+1;
		DBQuery::query("INSERT INTO {$database['prefix']}Teamblog (blogid,userid,acl,created,lastLogin) VALUES ('$newBlogId','$userid','16','$created','$lastLogin')");
		
		/* Insert new user setting into blogsetting */
		$blogLanguage = DBQuery::queryCell("SELECT value FROM {$database['prefix']}BlogSettings WHERE blogid='$blogid' AND name='blogLanguage'");
		$language = DBQuery::queryCell("SELECT value FROM {$database['prefix']}BlogSettings WHERE blogid='$blogid' AND name='language'");
		$timezone = DBQuery::queryCell("SELECT value FROM {$database['prefix']}BlogSettings WHERE blogid='$blogid' AND name='timezone'");

		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','allowWriteDblCommentOnGuestbook','1')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','allowWriteOnGuestbook','1')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','blogLanguage','$blogLanguage')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','commentsOnRSS','10')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','defaultDomain','$host')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','defaultEditor','modern')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','defaultFormatter','ttml')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','description','')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','entriesOnList','10')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','entriesOnPage','10')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','entriesOnRSS','10')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','language','$language')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','logo','')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','logoHeight','0')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','logoLabel','')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','logoWidth','0')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','name','$name')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','noneCommentMessage','')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','noneTrackbackMessage','')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','publishEolinSyncOnRSS','1')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','publishWholeOnRSS','1')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','singleCommentMessage','')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','singleTrackbackMessage','')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','timezone','$timezone')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','title','')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','useSloganOnCategory','1')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','useSloganOnPost','1')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','useSloganOnTag','1')");
		DBQuery::query("INSERT INTO {$database['prefix']}BlogSettings VALUES ('$newBlogId','visibility','2')");
		
		/* Insert new user setting into skin setting */
		$skinName = DBQuery::queryCell("SELECT skin FROM {$database['prefix']}SkinSettings WHERE blogid='$blogid'");
		DBQuery::query("INSERT INTO {$database['prefix']}SkinSettings VALUES ('$newBlogId','$skinName','10','10','5','5','10','1','5','1','1','30','30','30','30','30','1','1','1','1','1','base','000000','','FFFFFF','00ADEF','30','1')");
		
		DBQuery::query("DELETE FROM {$database['prefix']}UsersignupLogs WHERE loginid='{$loginid}' LIMIT 1");
		getRespondResultPage(1);
	} else {
		getRespondResultPage(0);
	}
}


function getRespondResultPage($error){
	header('Content-Type: text/xml; charset=utf-8');
	if($error==1) {
		print ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<response>\n<error>0</error>\n</response>");
	} elseif($error==0) {
		print ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<response>\n<error>1</error>\n</response>");
	}
	exit;
}

function HC_UserSignUpManager() {
	global $blog,$blogURL,$database,$blogid;
	require_once ('lang/pluginLocal.php');

	if( !Acl::check( array("group.creators", "group.administrators") ) ) {
		ob_start();
		?>
		<div id="multiblogsignuplist" class="part">
		<h2 class="caption"><span class="main-text"><?php echo pluginLang::plts('多人部落格管理');?></span></h2>
		<div><?php echo pluginLang::plts('錯誤::本項目僅限於最高權限使用者操作使用。');?></div>
		</div>
		<?php
		$view = ob_get_contents();
		ob_clean();
		flush();
		
	} else {
	
	ob_start();
	?>
		<style type="text/css">
		<!--
		#multiblogsignuplist table tr th {
			background-color: #ccc;
			padding: 4px;
			color: #fff;
		}
		#multiblogsignuplist table tr th.selectFirstTH {
			border-left: 1px solid #ccc;
			text-align: center;
		}
		#multiblogsignuplist table tr td {
			padding: 4px;
		}
		#multiblogsignuplist td.selectItems {
			border-right: 1px solid #ccc;
			border-bottom: 1px solid #ccc;
		}
		#multiblogsignuplist td.selectItemsFirst {
			border-bottom: 1px solid #ccc;
			border-left: 1px solid #ccc;
			border-right: 1px solid #ccc;
			text-align: center;
		}
		span.modifySignupUser {
			cursor:pointer;
		}
		-->
		</style>
		<script language="javascript" type="text/javascript">
		/*<![CDDA[*/
		function createBlogSignup(types, loginid) {
			try {
				if(types=="teamblog") {
					var request = new HTTPRequest("GET", "<?php echo $blogURL;?>/plugin/HC_signuptoy/createTeamBlogSignup/?loginid=" + encodeURIComponent(loginid));
				<?php
				if( Acl::check( array("group.creators") ) ) {
				?>
				} else {
					var request = new HTTPRequest("GET", "<?php echo $blogURL;?>/plugin/HC_signuptoy/createBlogSignup/?loginid=" + encodeURIComponent(loginid));
				<?php
				}
				?>
				}
				request.onSuccess = function() {
					PM.removeRequest(this);
					top.location.reload();
				}
				request.onError = function() {
					PM.removeRequest(this);
					PM.showErrorMessage("<?php echo _t('저장하지 못했습니다.');?>", "center", "bottom");
				}
				request.send("");
			} catch(e) {
			}
		}
		function deleteBlogSignup(loginid) {
			try {
				var request = new HTTPRequest("GET", "<?php echo $blogURL;?>/plugin/HC_signuptoy/deleteBlogSignup/?loginid=" + encodeURIComponent(loginid));
				request.onSuccess = function() {
					PM.removeRequest(this);
					top.location.reload();
				}
				request.onError = function() {
					PM.removeRequest(this);
					PM.showErrorMessage("<?php echo _t('저장하지 못했습니다.');?>", "center", "bottom");
				}
				request.send("");
			} catch(e) {
			}
		}
		/*]]>*/
		</script>
		<div id="multiblogsignuplist" class="part">
			<h2 class="caption"><span class="main-text"><?php echo pluginLang::plts('多人部落格管理');?></span></h2>
		<table cellpadding="0" cellspacing="0" border="0" width="90%">
		<tr><th class="selectFirstTH"><?php echo pluginLang::plts('類別');?></th><th><?php echo pluginLang::plts('申請帳號');?></th><th><?php echo pluginLang::plts('部落格識別名稱');?></th><th><?php echo pluginLang::plts('使用者名稱');?></th><th><?php echo pluginLang::plts('建立時間');?></th><th><?php echo pluginLang::plts('來源');?></th><th colspan="2"><?php echo pluginLang::plts('操作');?></th></tr>
	<?php
		if( Acl::check( array("group.creators") ) ) {
			$signuptype = '1=1';
		} else {
			$signuptype = "`signuptype`='teamblog' AND `host`={$blogid}";
		}
		$rows = DBQuery::queryAll("select loginid,name,created,signuptype,ip,host from {$database['prefix']}UsersignupLogs where {$signuptype}");
		foreach( $rows as $row ) {
			?>
			<tr>
			<?php
			if($row['signuptype']=='teamblog') {
				$username = $row['name'];
				$blogname = DBQuery::queryCell("select `value` from {$database['prefix']}BlogSettings where `blogid`={$row['host']} AND `name`='title'");
			?>
			<td class="selectItemsFirst">團隊成員
			<?php
			} else {
				$username = $row['name'];
				$blogname = $row['name'];
			?>
			<td class="selectItemsFirst">部落格
			<?php
			}
			?>
			</td><td class="selectItems"><?=$row['loginid'];?></td><td class="selectItems"><?=$blogname;?></td><td class="selectItems"><?=$username;?></td><td class="selectItems"><?=date("Y/m/d H:i:s",$row['created']);?></td><td class="selectItems"><?=$row['ip'];?></td><td class="selectItems"><span class="modifySignupUser" onclick="deleteBlogSignup('<?=$row['loginid'];?>');">刪除</span></td><td class="selectItems"><span class="modifySignupUser" onclick="createBlogSignup('<?=$row['signuptype'];?>','<?=$row['loginid'];?>');">開通</span></td></tr>
			<?php
		}
	?>
		</table></div>
	<?php
		$view = ob_get_contents();
		ob_clean();
		flush();
	}
	
	echo $view;
}
?>